<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>Finalove-App-About</title>
		<link rel="stylesheet" href="../resource/font/css/font-awesome.min.css">
		<link rel="stylesheet" href="../resource/css/mint.css">
		<link rel="stylesheet" href="../resource/css/style.css">
		<script src="../resource/js/vue.js"></script>
		<script src="../resource/js/mint.min.js"></script>
		<script src="../resource/js/jquery.min.js"></script>
		<style type="text/css">
			body{
				background-color: #f2f2f2;
			}
			.ct-box{
				margin-top: 40px;
			}
			.app-icon{
				margin-top: 30px;
				border-radius: 15px;
				border: 2px solid #ebeef5;
				box-shadow: 0 4px 12px 0 rgba(0,0,0,.1);
			}
			.mt-cell-box {
				width: 80%;
				margin-left: 10%;
			}
		</style>
	</head>
	<body>
		<div id="main">
			<mt-header fixed :title="title">
				<mt-button icon="back" slot="left" @click="closeView"></mt-button>
			</mt-header>
			<div class="ct-box">
				<p class="text-center">
					<img src="../resource/icon/app_icon.png" width="120px" height="120px" class="app-icon">
				</p>
				<h2 class="text-center">Finalove - {{about.version}}</h2>
				<div class="mt-cell-box" @click="updateLog">
					<mt-cell title="更新日志" is-link></mt-cell>
				</div>
				<div class="mt-cell-box" @click="agreement">
					<mt-cell title="用户须知" is-link></mt-cell>
				</div>
				<div class="mt-cell-box" @click="checkUpdate">
					<mt-cell title="检测更新" is-link>
						<span>当前版本：{{about.version}}</span>
					</mt-cell>
				</div>
			</div>
		</div>
	</body>
	<script type="text/javascript">
		var main = new Vue({
			el:"#main",
			data:{
				title:"关于Finalove",
				about:{
					version:"",
				}
			},
			mounted:function() {
				this.init();
			},
			methods:{
				init() {
					// 获取配置
					$.getJSON('../resource/config.json',function(res){
						main.api_prefix = res.api_prefix;
						main.api_key = res.api_key;
						main.app_version = res.app_version;
						main.app_version_check_url = res.app_version_check_url;
					});
					// loading
					document.addEventListener('plusready',function () {
						// 沉浸header
						plus.navigator.setFullscreen(true);
						// backbutton事件修改
						plus.key.addEventListener('backbutton', function() {
							plus.webview.currentWebview().close();
							return false;
						}, false);
						// 获取信息
						main.about = {
							version:main.app_version,
						}
					})
				},
				closeView() {
					plus.webview.currentWebview().close();
				},
				updateLog() {
					plus.webview.close("updateLog");
					plus.webview.open("log/updateLog.html","updateLog",{},"slide-in-right",200);
				},
				agreement() {
					plus.webview.close("userAgreement");
					plus.webview.open("me/userAgreement.html","userAgreement",{},"slide-in-right",200);
				},
				checkUpdate() {
					var url = this.app_version_check_url+"?t="+new Date().getTime();
					$.getJSON(url,function(res) {
						if(res.app_version != main.app_version){
							main.$messagebox({
								title:"更新提示",
								message:"发现版本是否现在更新？",
								confirmButtonText:"现在更新",
								cancelButtonText:"暂不更新",
								showCancelButton:true,
								closeOnClickModal:false
							}).then(action => {
								if (action == 'confirm') {
									var url = res.apk_url;//下载地址
									var dtask = plus.downloader.createDownload(url,{},function(d,status){
										if(status==200){
											main.$indicator.close();
											var path=d.filename;
											plus.runtime.install(path);
										}else{//下载失败
											alert('Download failed:'+status);
										}
									});
									dtask.addEventListener("statechanged",function(task,status){
										if(!dtask){
											return false;
										}
										switch(task.state){
											case 3:
												main.$indicator.open({text:"下载更新中，请勿关闭",spinnerType: 'double-bounce'});
												main.downloadingText = "已下载：" + (task.downloadedSize/1024/1024).toFixed(2) + " Mb";
												$(".mint-indicator-text").html('<span class="mint-indicator-text" style="">下载更新中，请勿关闭，'+main.downloadingText+'</span>')
												break;
											case 4: 
												main.$toast('下载完成');
												break;
										}
									});
									dtask.start();
								}
							}).catch();
						}else{
							main.$toast({message:"当前版本已是最新版本啦~",position:"bottom"});
						}
					})
				}
			}
		})
	</script>
</html>
